import { Common } from '../../common/common.js';
import { MyModel } from '../../../../modules/my.js';

const app = getApp();
const my = new MyModel();
const common = new Common();

Page({
  data: {
    projectName: null, // 项目名称
    entryTime: null, // 开始时间
    quitTime: null, //  结束时间
    position: null, // 担任职务
    projectDesc: null // 项目介绍
  },
  // 保存工作经历
  saveDemoTime(e) {
    let value = e.detail.value;
    let data = {
      resumeId: common.getRid(),
      projectName: value.projectName,
      beginTime: this.data.entryTime,
      endTime: this.data.quitTime,
      position: value.position,
      projectIntroduce: value.projectDesc
    }
    console.log(data)
    if(data['projectName'] == null || data['projectName'].trim() == '') {
      wx.showToast({
        title: '项目名称不能为空',
        icon: 'none'
      });
      return false
    }else if(data['beginTime'] == null) {
      wx.showToast({
        title: '开始时间不能为空',
        icon: 'none'
      });
      return false
    }else if(data['endTime'] == null) {
      wx.showToast({
        title: '结束时间不能为空',
        icon: 'none'
      });
      return false
    }else if(data['position'] == null || data['position'].trim() == '') {
      wx.showToast({
        title: '担任职务不能为空',
        icon: 'none'
      });
      return false
    }else if(data['projectIntroduce'] == null || data['projectIntroduce'].trim() == '') {
      wx.showToast({
        title: '项目介绍不能为空',
        icon: 'none'
      });
      return false
    }
    if(data['beginTime'] > data['endTime']) {
      wx.showToast({
        title: '开始时间不能大于结束时间!',
        icon: 'none'
      });
      return false
    }
    wx.getSetting({
      success: res => {
        if (!res.authSetting['scope.userInfo']) {
          wx.showModal({
            title: '提示',
            content: '请先授权',
            success: function (res) {
              if (res.confirm) {
                app.getLoginCode();
                wx.redirectTo({
                  url: '/pages/login/index',
                })
              } else if (res.cancel) {
                console.log('这个用户不想授权')
              }
            }
          })
        }else {
          console.log(data)
          my.getResumes()
          .then(res => {
            if(res.data != null) {
              my.addExperience(data)
              .then(res => {
                if(res.code == 2) {
                  wx.showToast({
                    title: '添加成功',
                    icon: 'none',
                    duration: 2000
                  });
                  setTimeout(() => {
                    wx.redirectTo({
                      url: '../index/index'
                    });
                  }, 2000)
                }else {
                  wx.showToast({
                    title: '添加失败，请稍后重试！',
                    icon: 'none',
                    duration: 2000
                  });
                }
              })
            }else {
              wx.showModal({
                title: "请先创建简历",
                content: "是否前去创建简历",
                mask: true,
                success: res => {
                  if (res.confirm) {
                    wx.redirectTo({
                      url: "/pages/my/createresume/index"
                    });
                  }
                }
              });
            }
          })
        }
      }
    })
  },
  bindChange: function(e) {
    let _this = this;
    switch(e.target.id) {
      // 开始时间
      case 'entrytime':
        _this.setData({
          entryTime: e.detail.value
        });
      break;
      // 结束时间
      case 'quittime':
        _this.setData({
          quitTime: e.detail.value
        });
      break;
    }
  }
})